#include <iostream>
#include <fstream>
#include <map>

struct FileHeaderStruct {
    size_t size;
};

struct FileDataStruct {
    int32_t key;
    char typeString[32];
    int32_t type;
    int32_t zIndex;
    struct Layout {
        int32_t x;
        int32_t y;
        int32_t w;
        int32_t h;
    } layout;
    enum class PositionType : int {
        RELATIVE,
        STATIC,
        FIXED,
    } position;
};

std::ostream &operator <<(std::ostream &os, const enum FileDataStruct::PositionType &position)
{
    os << static_cast<int32_t>(position);
    return os;
}

std::istream &operator >>(std::istream &is, enum FileDataStruct::PositionType &position)
{
    int32_t enumPosition;
    is >> enumPosition;
    position = static_cast<enum FileDataStruct::PositionType>(enumPosition);
    return is;
}

std::map<int32_t, FileDataStruct> datas;

void Read()
{
    std::ifstream ifs("file.bin", std::ifstream::in | std::ifstream::binary);
    struct FileHeaderStruct header;
    ifs >> header.size;
    struct FileDataStruct data = {};
    for (int32_t i = 0; i < header.size; i++) {
        ifs >> data.key >> data.typeString >> data.type >>
            data.zIndex >> data.layout.x >> data.layout.y >>
            data.layout.w >> data.layout.h >> data.position;
        datas[data.key] = data;
    }
}

void Write(int argc, const char **argv)
{
    std::ofstream ofs("file.bin", std::ofstream::out | std::ofstream::binary);
    ofs << datas.size() << std::endl;
    for (const auto &[key, data] : datas) {
        ofs << data.key << " " << data.typeString << " " << data.type << " " <<
            data.zIndex << " " << data.layout.x << " " << data.layout.y << " " <<
            data.layout.w << " " << data.layout.h << " " << data.position << std::endl;
    }
    ofs.close();
}

int main(int argc, const char **argv)
{
    if (argc != 1) {
        Read();
        for (const auto &[key, data] : datas) {
            std::cout << data.typeString << " " << data.layout.x << " " << data.position << std::endl;
        }
    } else {
        datas[0] = {0, "0", 0, 0, {0, 0, 0, 0}, FileDataStruct::PositionType::STATIC};
        datas[1] = {1, "1", 1, 1, {1, 1, 1, 1}, FileDataStruct::PositionType::FIXED};
        datas[2] = {2, "2", 2, 2, {2, 2, 2, 2}, static_cast<enum FileDataStruct::PositionType>(-1)};
        Write(argc, argv);
    }
    return 0;
}
